﻿
using System;

using XF.Common.WeChat;


namespace TestTest
{
    class Program
    {
        private static readonly string corpid = "ww267f792e50eb8c08";
        private static readonly string secret = "2Fi7VR3CsjnCfQ0C-Z6JKndoqkmBL66aN4TVYrdNZVc";
       
      //  public AbsFactoryTE abs = (AbsFactoryTE)AbsFactory.CreateInterface();
        static void Main(string[] args)
        {
            //   var entity = new ExtendMeeting()
            //   {
            //       BeginTime = DateTime.Now,
            //       CRoomID = "1",
            //       CRoomIDs = "2",
            //       EndTime = DateTime.Now.AddHours(-8),
            //        Extend=1,
            //         ID=1
            //   };
            //   string xml = TableToList.XMLSerialize(entity, Encoding.UTF8);
            //   var bpmEetity = new BPMProcess() {
            //       ProcessNo = "Meeting001",
            //       strBOID = Guid.NewGuid().ToString(),
            //        strBSID="ese",
            //         strBTID="eseCode",
            //          strCurrentUserId="admin",
            //           strTopic="会议预约",
            //            strXML= xml
            //   };
            //   var bpm = new BS_Service();
            //   //发起审批
            //var  result= bpm.StartProcess(bpmEetity.strBSID,bpmEetity.strBTID, bpmEetity.strBOID, bpmEetity.strCurrentUserId, bpmEetity.strTopic, bpmEetity.ProcessNo, bpmEetity.strXML);
            //   var res = TableToList.XMLDeSerialize<response>(result);
            //   var returnStr = TableToList.XMLSerialize(result, Encoding.UTF8);
            //   Console.WriteLine();
           // WeChatHelper wx = new WeChatHelper(corpid, secret);
         //   wx.SendTextMessage();
          var aaa= GetAccessToken(corpid,secret);
        // var USERID=   GetUserId(aaa,"-wXFkysMCD7MZqwiIidBNdrKYRt3sP1w3-nphbwJYhg");
       //     GetUserInfo(aaa, USERID);
         var ccc= GetUserInfo(aaa,"tanliang");
            var msg = new Message(new XF.Common.WeChat.Text($"请您参加于举行的由主持的主题会议，望准时参加!"), "xunfu-yunlongge2");
            //    Console.WriteLine(ccc);
            SendTextMessage(aaa, msg);
            Console.ReadKey();
        }
        public static void SendTextMessage(string ACCESS_TOKEN,Message message)
        {
            HttpClientHelper request = new HttpClientHelper();
            message.agentid = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["agentid"]);
            var response = request.Post($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={ACCESS_TOKEN}", Newtonsoft.Json.JsonConvert.SerializeObject(message));
        }
        public  static string GetAccessToken(string corpid, string secret)
        {
            HttpClientHelper request = new HttpClientHelper();
            var response = request.Get($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}");
            // Response.Write(response);
            var aaa = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response);
            Console.WriteLine(aaa.access_token+"-------"+aaa.expires_in);
            return aaa.access_token;
        }
        public static string GetUserId(string ACCESS_TOKEN, string Code)
        {
            HttpClientHelper request = new HttpClientHelper();
            var response = request.Get($"https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={ACCESS_TOKEN}&code={Code}");
            Console.WriteLine(response);
            var aaa = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response);
            return aaa.UserId;
        }
        public static string GetUserInfo(string ACCESS_TOKEN,string UserId)
        {
            HttpClientHelper request = new HttpClientHelper();
            var response = request.Get($"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={ACCESS_TOKEN}&userid={UserId}");
            //  var aaa = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response);
            Console.WriteLine(response);
            return response;
        }

    }
}
